package message

import (
	"fmt"
	"time"

	"github.com/meilihao/golib/v2/misc"
)

const (
	TypeNeedReplyMessage = "NeedReplyMessage"
)

var (
	_ NeedReplyMessage = new(NeedReplyMessageImpl)
)

type NeedReplyMessage interface {
	error
	Message
	ConfigurableTimeoutMessage
}

type NeedReplyMessageImpl struct {
	Timeout         time.Duration
	MessageDeadLine time.Time
	SystemTags      []string
	UserTags        []string

	*MessageImpl
}

func (impl *NeedReplyMessageImpl) HasSystemTag(tag string) bool {
	if len(impl.SystemTags) == 0 {
		return false
	}

	return misc.IsInStrings(tag, impl.SystemTags)
}

func (impl *NeedReplyMessageImpl) AddSystemTag(systemTag string) {
	if impl.SystemTags == nil {
		impl.SystemTags = make([]string, 0)
	}
	impl.SystemTags = append(impl.SystemTags, systemTag)
}

func (impl *NeedReplyMessageImpl) GetTimeout() time.Duration {
	return impl.Timeout
}

func (impl *NeedReplyMessageImpl) SetTimeout(timeout time.Duration) {
	impl.Timeout = timeout
}

func (impl *NeedReplyMessageImpl) GetMessageDeadline() time.Time {
	return impl.MessageDeadLine
}

func (impl *NeedReplyMessageImpl) SetMessageDeadline(messageDeadline time.Time) {
	impl.MessageDeadLine = messageDeadline
}

func (impl *NeedReplyMessageImpl) Error() string {
	return fmt.Sprintf("Message[name: %s, id: %s] timeout after %d seconds",
		impl.GetClass(),
		impl.GetId(),
		int64(impl.Timeout.Seconds()),
	)
}
